package com.yyfax.ph.personal.authority.bankcard.presenter;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.secure.RSA;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.GlobalVariableConfig;
import com.yyfax.ph.personal.authority.RSAPresenter;
import com.yyfax.ph.personal.authority.bankcard.model.BindCardModel;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BindCardPresenter extends RSAPresenter<BindCardModel, BindCardPresenter.View> {
	private static final String TAG = BindCardPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<BindCardPresenter> {
		@Override
		public BindCardPresenter create() {
			return new BindCardPresenter();
		}
	}

	public interface View extends IView {

		void bindCardSuccess(String tips, String name, String cardNumber, String bankName, String bankCode);

		void bindCardFailure(String tips);

	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new BindCardModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Override
	public Object getRSAParams(Object params, int functionalCode) {
		BindCardModel.Card card = (BindCardModel.Card) params;
		String keyRSA = GlobalVariableConfig.getKeyRSA();
		card.cardId = RSA.encryptByPublicKey(card.cardId, keyRSA);
		card.certId = RSA.encryptByPublicKey(card.certId, keyRSA);
		card.phone = RSA.encryptByPublicKey(card.phone, keyRSA);
		return card;
	}

	public void bindCard(String name, String certId, String phone, String bankCode, String cardId) {
		BindCardModel.Card card = new BindCardModel.Card();
		card.bankCode = bankCode;
		card.cardId = cardId;
		card.certId = certId.toUpperCase();
		card.name = name;
		card.phone = phone;
		CLog.i(TAG, GlobalVariableConfig.getKeyRSA() + "公钥");
		CLog.i(TAG, card.cardId + "银行卡");
		CLog.i(TAG, card.certId + "身份证");
		CLog.i(TAG, card.phone + "电话号");
		execute(card, BindCardModel.msg_bind_bank);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void bindCardResult(BindCardModel.CardResult result) {
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E027, result.getStatus());
		String msg = result.getMsg();
		if (isResponseSuccess(result)) {
			mView.bindCardSuccess("绑定成功", result.getContent().name, result.getContent().cardId, result.getContent().bankName, result.getContent().bankCode);
			CLog.i(TAG, "名字:  " + result.getContent().name);
			CLog.i(TAG, "卡号:  " + result.getContent().cardId);
			CLog.i(TAG, "银行名字:  " + result.getContent().bankName);
		} else {
			mView.bindCardFailure(TextUtils.isEmpty(msg) ? "银行卡信息有误或不支持该卡，请核对后再试或更换其他卡!" : msg);
		}
	}
}
